﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using fanfu.Common;
using fanfu.Model;

namespace fanfu.Business
{
    public class AdminUserRule
    {
        #region 获取所有用户信息
        /// <summary>
        /// 获取所有用户信息
        /// </summary>
        public DataTable GetAllUserInfo()
        {
            string sqlString = "SELECT * FROM AdminUser ORDER BY RegistTime DESC";
            DataTable dt = SqlHelper.GetDataTable(sqlString);
            return dt;
        }
        #endregion

        #region 获取指定userName的用户
        /// <summary>
        /// 获取指定userName的用户
        /// </summary>
        /// <param name="userName"></param>
        /// <returns></returns>
        public AdminUserModel GetUserInfoByName(string userName)
        {
            string sqlString = "SELECT TOP 1 * FROM AdminUser WHERE UserName=@UserName";
            SqlParameter para = new SqlParameter("@UserName", userName);
            DataTable dt = SqlHelper.GetDataTable(sqlString,para);
            if (dt.Rows.Count == 1)
                return new AdminUserModel(dt.Rows[0]);
            else
                return new AdminUserModel();
        }
        #endregion

        #region 获取指定ID的用户
        /// <summary>
        /// 获取指定ID的用户
        /// </summary>
        /// <param name="ID"></param>
        /// <returns></returns>
        public AdminUserModel GetUserInfoByID(string ID)
        {
            string sqlString = "SELECT TOP 1 * FROM AdminUser WHERE ID=@ID";
            SqlParameter para = new SqlParameter("@ID", ID);
            DataTable dt = SqlHelper.GetDataTable(sqlString, para);
            if (dt.Rows.Count == 1)
                return new AdminUserModel(dt.Rows[0]);
            else
                return new AdminUserModel();
        }
        #endregion

        #region 删除指定ID的用户
        /// <summary>
        /// 删除指定ID的用户
        /// </summary>
        /// <param name="ID"></param>
        /// <returns></returns>
        public int DeleteUser(string ID)
        {
            string sqlString = string.Format("UPDATE AdminUser SET Flag={0} WHERE ID={1}",(int)AdminUserFlag.delete,ID);
            return SqlHelper.ExecuteCommand(sqlString);
        }
        #endregion

        #region 后台账号登录
        /// <summary>
        /// 后台账号登录
        /// </summary>
        /// <param name="userName"></param>
        /// <param name="userPwd"></param>
        /// <returns></returns>
        public AdminUserModel UserLogin(string userName, string userPwd)
        {
            string sqlString = "SELECT * FROM AdminUser WHERE UserName=@UserName AND UserPassword=@UserPassword AND Flag<>@Flag";
            List<SqlParameter> paraList = new List<SqlParameter>();
            paraList.Add(new SqlParameter("@UserName",userName));
            paraList.Add(new SqlParameter("@UserPassword", userPwd));
            paraList.Add(new SqlParameter("@Flag",(int)AdminUserFlag.delete));
            DataTable dt = SqlHelper.GetDataTable(sqlString, paraList.ToArray());
            if (dt.Rows.Count == 1)
                return new AdminUserModel(dt.Rows[0]);
            else
                return new AdminUserModel();
        }
        #endregion

        #region 账号解锁
        /// <summary>
        /// 账号解锁
        /// </summary>
        /// <param name="userName"></param>
        public void UserExit(string userName)
        {
            string sqlString = string.Format("UPDATE AdminUser SET Flag={0} WHERE UserName='{1}'",
                (int)AdminUserFlag.normal, userName);
            SqlHelper.ExecuteCommand(sqlString);
        }
        #endregion

        #region 更新登陆次数,时间,LoginIP,锁定账号
        /// <summary>
        /// 更新登陆次数,时间,LoginIP,锁定账号
        /// </summary>
        /// <param name="lastLoginIP"></param>
        public void UpdateLoginMessage(string lastLoginIP, string userName)
        {
            string sqlString = string.Format("UPDATE AdminUser SET LastLoginTime=GETDATE(), LoginCount=LoginCount+1, LastLoginIP='{0}', Flag={1} WHERE UserName='{2}'",
                lastLoginIP, (int)AdminUserFlag.normal, userName);
            SqlHelper.ExecuteCommand(sqlString);
        }
        #endregion

        #region 用户状态枚举值转换
        /// <summary>
        /// 用户状态枚举值转换
        /// </summary>
        public string GetFlagDes(object objFlag)
        {
            string strResult = string.Empty;
            int flag = Convert.ToInt32(objFlag);
            switch (flag)
            {
                case (int)AdminUserFlag.normal: strResult = "正常"; break;
                case (int)AdminUserFlag.locked: strResult = "锁定"; break;
                case (int)AdminUserFlag.delete: strResult = "删除"; break;
            }
            return strResult;
        }
        #endregion
    }
}
